package aip.assignment2.persistence.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Stone
 */
@Entity
@Table(name = "customer")
@XmlRootElement
@NamedQueries(
{
	@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
	@NamedQuery(name = "Customer.findById", query = "SELECT c FROM Customer c WHERE c.id = :id"),
	@NamedQuery(name = "Customer.findByEmail", query = "SELECT c FROM Customer c WHERE c.email = :email")
})
public class Customer implements Serializable
{
	private static final long serialVersionUID = 1L;
	@Id
	@SequenceGenerator(name = "customer_id", allocationSize = 1, sequenceName = "customer_id_seq")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_id")
	@Basic(optional = false)
	@Column(name = "id")
	private Integer id;
	// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
	@Basic(optional = false)
	@NotNull
	@Size(min = 1, max = 200)
	@Column(name = "email")
	private String email;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
	private List<Purchase> purchaseList;
	@JoinColumn(name = "name", referencedColumnName = "id")
	@ManyToOne(optional = false)
	private PersonName name;
	@JoinColumn(name = "credit_card", referencedColumnName = "id")
	@ManyToOne(optional = false)
	private CreditCard creditCard;
	@JoinColumn(name = "address", referencedColumnName = "id")
	@ManyToOne(optional = false)
	private Address address;
	public Customer()
	{
	}
	public Customer(Integer id)
	{
		this.id = id;
	}
	public Customer(Integer id, String email)
	{
		this.id = id;
		this.email = email;
	}
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}
	public String getEmail()
	{
		return email;
	}
	public void setEmail(String email)
	{
		this.email = email;
	}
	@XmlTransient
	public List<Purchase> getPurchaseList()
	{
		return purchaseList;
	}
	public void setPurchaseList(List<Purchase> purchaseList)
	{
		this.purchaseList = purchaseList;
	}
	public PersonName getName()
	{
		return name;
	}
	public void setName(PersonName name)
	{
		this.name = name;
	}
	public CreditCard getCreditCard()
	{
		return creditCard;
	}
	public void setCreditCard(CreditCard creditCard)
	{
		this.creditCard = creditCard;
	}
	public Address getAddress()
	{
		return address;
	}
	public void setAddress(Address address)
	{
		this.address = address;
	}
	@Override
	public int hashCode()
	{
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}
	@Override
	public boolean equals(Object object)
	{
		// TODO: Warning - this method won't work in the case the id fields are not set
		if (!(object instanceof Customer))
		{
			return false;
		}
		Customer other = (Customer) object;
		if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
		{
			return false;
		}
		return true;
	}
	@Override
	public String toString()
	{
		return "aip.assignment2.data.entity.Customer[ id=" + id + " ]";
	}
}
